<?php

namespace app\api\controller;

use app\api\model\User as UserModel;
use think\Db;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $model = new UserModel;
        $user_id = $model->login($this->request->post());
        $user_info['team_id'] = 0;
        if ($user_id) {
            $user_info = Db::name('user')->where('user_id', $user_id)->find();
        }
        // if ($user_info['is_pay'] == 0) {//到期没交会费的
        //     $user_info['team_id'] = 0;
        // }
        return $this->renderSuccess([
            'user_id'     => $user_id,
            'token'       => $model->getToken(),
            'team_id'     => $user_info['team_id'],     //服务队id
            'shanghui_id' => $user_info['shanghui_id'], //商会ID
        ]);
    }
     /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function getUserInfo()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        $userInfo['ruhui'] = Db::name('shanghui_user')->where('status',10)->where('user_id', $userInfo['user_id'])->find();
        $userInfo['shanghui'] = Db::name('shanghui')->where('status',10)->where('userid', $userInfo['user_id'])->find();
        return $this->renderSuccess(compact('userInfo'));
    }
    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }

    /**
     * 用户编辑个人信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = input('param.');
        $userInfo = $this->getUser();
        try {
            $res = Db::name('user')
                ->where('user_id', '=', $userInfo['user_id'])
                ->update([
                    'company_name' => $param['company'] ?? $userInfo['company_name'],
                    'user_address' => $param['userAddress'] ?? $userInfo['user_address'],
                    'area' => $param['userArea'] ?? $userInfo['area'],
                    'nickName' => $param['userName'] ?? $userInfo['nickName'],
                    'card_id' => $param['userNumber'] ?? $userInfo['card_id'],
                    'gender' => $param['userSex'] ?? $userInfo['gender'],
                    'phone' => $param['userTel'] ?? $userInfo['phone'],
                    'birth_date' => $param['userbirthday'] ?? $userInfo['birth_date'],
                ]);
        } catch (\Exception $e) {
            return $this->renderError('编辑失败');
        }
        return $this->renderSuccess([], '编辑成功');
    }

    /**
     * 积分排行
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function pointsRanking()
    {
        // 当前用户信息
        $user = $this->getUser();
        $page = 1;
        $param = input('param.');
        if (isset($param['page'])) {
            $page = $param['page'];
        }
        $where = ['su.status' => 90, 'su.shanghui_id' => $user['shanghui_id']];
        if (isset($param['is_team']) && $param['is_team'] == 1) {
            $where["su.team_id"] =  $user['team_id'];
        }
        $points_ranking = Db::name('shanghui_user')->alias('su')
        ->field('su.*,u.nickName')
        ->join('user u','su.user_id = u.user_id','LEFT')
        ->where($where)
        ->order(['su.points' => 'desc', 'su.add_date' => 'ASC'])
        ->select()->toArray();
        foreach ($points_ranking as $k => &$v) {
            $v['ranking'] = $k + 1;
            if ($v['user_id'] == $user['user_id']) {
                $user['my_ranking'] = $k + 1;
            }
        }
        $last_page = ceil(count($points_ranking) / 100);
        $points_ranking = array_slice($points_ranking, ($page - 1) * 100, 100);
        return $this->renderSuccess(compact('points_ranking', 'user', 'last_page'));
    }

    /**
     * 捐款排行
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function donateRanking()
    {
        // 当前用户信息
        $user = $this->getUser();
        $page = 1;
        $param = input('param.');
        if (isset($param['page'])) {
            $page = $param['page'];
        }
        $where = ['su.status' => 90, 'su.shanghui_id' => $user['shanghui_id']];
        if (isset($param['is_team']) && $param['is_team'] == 1) {
            $where["su.team_id"] =  $user['team_id'];
        }
        $donate_ranking1 = Db::name('shanghui_user')->alias('su')
        ->field('su.*,u.nickName')
        ->join('user u','su.user_id = u.user_id','LEFT')
        ->where($where)
        ->order(['su.donate' => 'desc', 'su.add_date' => 'ASC'])
        ->select()->toArray();

        foreach ($donate_ranking1 as $k => &$v) {
            $v['ranking'] = $k + 1;
            if ($v['user_id'] == $user['user_id']) {
                $user['my_ranking'] = $k + 1;
            }
        }
        $last_page = ceil(count($donate_ranking1) / 100);
        $donate_ranking = array_slice($donate_ranking1, ($page - 1) * 100, 100);
        return $this->renderSuccess(compact('donate_ranking', 'user', 'last_page'));
    }

}
